package model

import (
	"context"
	"ginblog/utils"
	"ginblog/utils/errormsg"
	"net/http"
)
import "github.com/qiniu/go-sdk/v7/storage"
import "github.com/qiniu/go-sdk/v7/auth/qbox"
import "mime/multipart"

var AccessKey = utils.AccessKey
var SecretKey = utils.SecretKey
var Bucket = utils.Bucket
var ImgUrl = utils.QiniuServer

func UpLoadFile(file multipart.File, fileSize int64) (string, int) {
	putPolicy := storage.PutPolicy{
		Scope: Bucket,
	}
	mac := qbox.NewMac(AccessKey, SecretKey)
	upToken := putPolicy.UploadToken(mac)

	cfg := storage.Config{
		Zone:          &storage.ZoneHuabei, // 华北（河北）
		UseCdnDomains: false,
		UseHTTPS:      false,
	}

	putExtra := storage.PutExtra{}

	formUploader := storage.NewFormUploader(&cfg)
	ret := storage.PutRet{}

	err := formUploader.PutWithoutKey(context.Background(), &ret, upToken, file, fileSize, &putExtra)

	if err != nil {
		return "", errormsg.ERROR
	}

	// http://t2dfv8cag.hb-bkt.clouddn.com/Fi4PvZTrMypH2c79Lf8jDI00csQJ
	// 这里的访问路径直接进行拼接 必须通过 http(s)://域名 + Key 来访问
	url := ImgUrl + ret.Key
	return url, http.StatusOK
}
